import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void StudentSystemStart() {
        ArrayList<Student> list = new ArrayList<>();
        while (true) {
            System.out.println("----------欢迎来到陈昊的学生管理系统------------------");
            System.out.println("1：添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出");
            Scanner sc = new Scanner(System.in);
            String choise = sc.next();
            switch (choise) {
                case "1" -> addStudent(list);
                case "2" -> delStudent(list);
                case "3" -> updateStudent(list);
                case "4" -> cxStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    System.exit(0);
                }
                default -> System.out.println("没有这个选项.请重新输入");
            }
        }
    }

    private static void cxStudent(ArrayList<Student> list) {
        int len = list.size();
        if (len == 0) {
            System.out.println("当前无学生信息，请添加后查询");
            return;
        }
        //到这里说明有的学生
        System.out.println("id\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getDz());
        }
    }

    private static void updateStudent(ArrayList<Student> list) {
        System.out.println("请输入你要修改的id");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = getindex(list, id);
        if (index == -1) {
            System.out.println("要修改的id" + id + "不存在");
            return;
        }
        //id存在走这里
        //获取要修改的id信息
        Student stu = list.get(index);
        //输入其他信息并且修改
        System.out.println("请输入你的新名字");
        String newName = sc.next();
        stu.setName(newName);
        System.out.println("请输入你的新年龄");
        int age = sc.nextInt();
        stu.setAge(age);
        System.out.println("请输入你的新家庭地址");
        String Dz = sc.next();
        stu.setDz(Dz);

    }

    private static boolean contains(ArrayList<Student> list, String id) {
        return getindex(list, id) > 0;
    }

    private static int getindex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (s.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

    private static void delStudent(ArrayList<Student> list) {
        System.out.println("请输入你要删除的id");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = getindex(list, id);
        if (index >= 0) {
            System.out.println("id为:" + id + "删除成功");
            list.remove(index);
        } else {
            System.out.println("id" + id + "不存在");
            return;
        }


    }

    private static void addStudent(ArrayList<Student> list) {
        Student stu = new Student();
        System.out.println("请输入学生的id");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        //id唯一
        boolean flag = contains(list, id);
        if (flag) {
            System.out.println("id重复,请重新输入");
            return;
        } else {
            stu.setId(id);
        }
        System.out.println("请输入学生的的姓名");
        String name = sc.next();
        stu.setName(name);
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        stu.setAge(age);
        System.out.println("请输入学生的家庭地址");
        String Dz = sc.next();
        stu.setDz(Dz);
        //将对象添加在集合中
        list.add(stu);
    }
}
